//============================================================================
// Copyright (C) 2013 Brett R. Jones
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================
#include "vx_debug.h"
#include <string.h>

//============================================================================--
//! Safe string copy.. logs error if buffer to short
int VxSafeStrCopy( char * pDest, const char * pSrc, int iBufLen )
{
	vx_assert( pDest );
	vx_assert( pSrc );
	if( (int)strlen( pSrc ) < iBufLen )
	{
		// safe to do string copy
		strcpy( pDest, pSrc );
		return 0;
	}
	// string is to long
	strncpy( pDest, pSrc, iBufLen - 1 );
	pDest[ iBufLen - 1] = 0;
	log_msg( 0, "VxUtilLib:Unsafe string copy to 0x%x\n", pDest );
	return -1;
}
//============================================================================--
//! return pointer to first char that is not whitespace
char * TrimLeadingSpaces( char *pString )
{
    while(0x20 == pString[0] || '\t' == pString[0] )
    {
        pString++;
        if(('\0' == pString[0]) || ('\n' == pString[0]))
        {
            return NULL;
        }
    }
    return pString;
}
#ifdef WIN_PLATFORM
//============================================================================--
/// strsep does not exist in windows and only some other platforms so
/// make our own
char *strsep(char **ppStr, const char *pDelimiters)
{
	char *pStart;
	char *pEnd;

	pStart = *ppStr;
	if (pStart == NULL)
	{
		return NULL;
	}
	if (pDelimiters[0] == '\0' || pDelimiters[1] == '\0')
	{
		char ch = pDelimiters[0];

		if (ch == '\0')
		{
			pEnd = NULL;
		}
		else
		{
			if (*pStart == ch)
			{
				pEnd = pStart;
			}
			else if (*pStart == '\0')
			{
				pEnd = NULL;
			}
			else
			{
				pEnd = strchr (pStart + 1, ch);
			}
		}
	}
	else
	{
		pEnd = strpbrk (pStart, pDelimiters);
	}
	if (pEnd)
	{
		*pEnd++ = '\0';
		*ppStr = pEnd;
	}
	else
	{
		*ppStr = NULL;
	}
	return pStart;

}
#endif //WIN_PLATFORM


